﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using SYHParamter;
using UtilityLibrary.Paramater;

namespace SYHWeb.Controllers
{
    public abstract class InterFaceController<T, U> : Controller where U : new()
    {
       protected String ProjectNumber
        {
            get
            {
                return ConfigurationManager.AppSettings["ProjectNumber"];
            }
        }

        protected String ProjectKey
        {
            get
            {
                return ConfigurationManager.AppSettings["ProjectKey"];
            }
        }

        public virtual ActionResult Index()
        {
            return Content("尚未编写接口说明。");
        }

        public virtual String Invoke(T requestParamater)
        {
            RequestParamater<U> reqParamater = requestParamater as RequestParamater<U>;

            //创建响应参数
            U u = new U();
            ResponseParamater responseParamater = u as ResponseParamater;
            responseParamater.Load<U>(reqParamater, ProjectNumber);


            Response.ContentType = "application/json";
            return DoInvoke(requestParamater, u);
        }

        /// <summary>
        /// 验证参数后，执行Invoke的处理过程
        /// </summary>
        /// <returns></returns>
        protected abstract String DoInvoke(T requestParamater, U responseParamater);

        public virtual ActionResult Debug()
        {
            //只允许本地调试
            if (!Request.IsLocal) return RedirectToAction("Index");

            return DoDebug();
        }

        /// <summary>
        /// 验证参数后，执行Debug的处理过程
        /// </summary>
        /// <returns></returns>
        protected abstract ActionResult DoDebug();
    }

}
